package com.ts.animal.service.impl;

import com.ts.animal.constant.AnimalConstant;
import com.ts.animal.dao.ExRfidLabelUseInfoDao;
import com.ts.common.service.IWaterCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ts.animal.dao.RfidLabelSettlementDao;
import com.ts.animal.domain.RfidLabelSettlementDO;
import com.ts.animal.service.RfidLabelSettlementService;
import com.ts.common.utils.Result;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class RfidLabelSettlementServiceImpl implements RfidLabelSettlementService {
    @Autowired
    private RfidLabelSettlementDao rfidLabelSettlementDao;
    @Autowired
    private ExRfidLabelUseInfoDao exRfidLabelUseInfoDao;
    @Resource
    private IWaterCodeService iWaterCodeService;

    @Override
    public RfidLabelSettlementDO get(Integer id) {
        return rfidLabelSettlementDao.get(id);
    }

    @Override
    public List<Map<String, Object>> list(Map<String, Object> map) {
        return exRfidLabelUseInfoDao.labelSettlementList(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return rfidLabelSettlementDao.count(map);
    }

    @Override
    public int save(Result result, RfidLabelSettlementDO rfidLabelSettlement) {
        int ret = rfidLabelSettlementDao.save(rfidLabelSettlement);
        if (ret == 0) {
            result.set(Result.FAIL, "保存失败", null);
        }
        return ret;


    }

    @Override
    public int update(Result result, RfidLabelSettlementDO rfidLabelSettlement) {
        int ret = rfidLabelSettlementDao.update(rfidLabelSettlement);
        if (ret == 0) {
            result.set(Result.FAIL, "更新失败", null);
        }
        return ret;
    }

    @Override
    public int batchRemove(Result result, Integer[] ids) {
        for (Integer id : ids) {
            RfidLabelSettlementDO rfidLabelSettlementDO = rfidLabelSettlementDao.get(id);
            exRfidLabelUseInfoDao.revokeLabelSettlementId(rfidLabelSettlementDO.getId());
            rfidLabelSettlementDao.remove(id);
        }
        return 1;
    }

    @Override
    @Transactional
    public int settlement(Result result) throws Exception {
        String settlementBatchNum = iWaterCodeService.generate("J", "settlementLabel", 8);
        RfidLabelSettlementDO rfidLabelSettlementDO = new RfidLabelSettlementDO();
        rfidLabelSettlementDO.setSettlementBatchNum(settlementBatchNum);
        rfidLabelSettlementDO.setSettlementDatetime(new Date());
        rfidLabelSettlementDO.setVerifyStatus(AnimalConstant.verify_status_not);
        int ret = rfidLabelSettlementDao.save(rfidLabelSettlementDO);
        if (ret == 0) {
            result.set(Result.FAIL, "结算失败", null);
        } else {
            exRfidLabelUseInfoDao.updateLabelSettlementId(rfidLabelSettlementDO.getId(), AnimalConstant.use_status_yes);
        }
        return ret;
    }

    @Override
    public int verify(Result result, Integer[] ids) {
        for (Integer id : ids) {
            RfidLabelSettlementDO rfidLabelSettlementDO = new RfidLabelSettlementDO();
            rfidLabelSettlementDO.setVerifyStatus(AnimalConstant.verify_status_yes);
            rfidLabelSettlementDO.setVerifyDatetime(new Date());
            rfidLabelSettlementDO.setId(id);
            rfidLabelSettlementDao.update(rfidLabelSettlementDO);
        }
        return 1;
    }
}
